package client;

import java.rmi.AccessException;
import java.util.Scanner;

public class Client {
	public static void main(String args[]) {
		if (args.length != 2) {
			System.err.println("Params: hostname certpath");
			System.exit(1);
		}
		Client c = new Client(/* Certificate */args[1]);
		c.start(/* Host */args[0]);
	}

	private Connection connection;
	private String certPath;
	private boolean editPermission;

	public Client(String certPath) {
		connection = new Connection();
		this.certPath = certPath;
	}

	/**
	 * Sätter upp anslutning till servern
	 */
	private void start(String hostName) {
		System.out.println("Connecting to server...");
		if (connection.connect(hostName)) {
			run();
		} else {
			System.err.println("Connection failed");
		}
	}

	/**
	 * Visar meny, hanterar val
	 */
	private void run() {
		int choice = -1;
		while (choice != 0) {
			printMenu();
			choice = handleChoice();
		}
		System.out.println("Exiting");
		System.out.println("Exited - Goodbye ");
	}

	/**
	 * Print menu
	 */
	private void printMenu() {
		System.out.println("\nAvailable options: ");
		System.out.println("[1] Show available journals");
		System.out.println("[2] View a journal");
		System.out.println("[3] Edit a journal");
		System.out.println("[4] Create a journal");
		System.out.println("[5] Delete a journal");
		System.out.println("[0] Exit");
		System.out.print("\n\n Choice: ");
	}

	private int handleChoice() {
		int choice = -1;
		Scanner scan = new Scanner(System.in);
		String temp = scan.next();
		try {
			choice = Integer.parseInt(temp);
		} catch (NumberFormatException e) {
			System.out.println("Not valid input: " + temp);
		}

		switch (choice) {
		case 1:
			listJournals();
			break;
		case 2:
			showJournal();
			break;
		case 3:
			editJournal();
			break;
		case 4:
			createJournal();
			break;
		case 5:
			deleteJournal();
			break;
		case 0:
			break;
		}
		return choice;
	}

	private void listJournals() {
		try {
			System.out.println("Contacting server... [View list]");
			String list = connection.getList();
			System.out.println("Success [View List]");
			System.out.println(list);
		} catch (AccessException e) {
			System.out.println("Failed [View List]");
			e.printStackTrace();
		}

	}

	private void showJournal() {
		try {
			String journal = getJournal();
			System.out.println("Loading journal...");
			EditWindow w = new EditWindow();
			w.showText(journal);
		} catch (AccessException e) {
			System.out.println("Failed [View]");
			e.printStackTrace();
		}
		
	}

	private String getJournal() throws AccessException {
		Scanner scan = new Scanner(System.in);
		System.out.print("\nEnter journal name: ");
		String journalName = scan.next();
		System.out.println("Contacting server... [View journal]");
		String journal = connection.viewJournal(journalName);
		
		return journal;
	}

	/**
	 * Edit a journal
	 */
	private void editJournal() {
		try {
			String journal = getJournal();
			System.out.println("Loading journal...");
			EditWindow w = new EditWindow();
			journal = w.editText(journal);
			try {
				System.out.println("Contacting server... [Edit]");
				connection.editJournal(journal);
				System.out.println("Success [Edit].");
			} catch (AccessException e) {
				System.out.println("Failed [Edit].");
				e.printStackTrace();
			}
		} catch (AccessException e) {
			System.out.println("Failed [View].");
			e.printStackTrace();
		}
	}

	private void createJournal() {
		Scanner scan = new Scanner(System.in);
		System.out.println("Patients name: ");
		String patient = scan.nextLine();
		System.out.println("Nurse name: ");
		String nurse = scan.nextLine();
		try {
			connection.createJournal(patient, nurse);
			System.out.println("Success [Create].");
		} catch (AccessException e) {
			System.out.println("Failed [Create].");
			e.printStackTrace();
		}
	}

	private void deleteJournal() {
		Scanner scan = new Scanner(System.in);
		System.out.println("Journal id: ");
		int id = Integer.parseInt(scan.nextLine());
		try {
			connection.deleteJournal(id);
			System.out.println("Success [Delete].");
		} catch (AccessException e) {
			System.out.println("Failed [Delete].");
			e.printStackTrace();
		}
	}
}
